<!DOCTYPE html>
<html lang="">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta itemprop="name" content="使用K6对接口进行压测">
<meta itemprop="description" content="使用K6对接口进行压测 背景  在实际开发中，常常需要对接口进行性能测试，因此，少不了压测工具
  在网上找了几款压测工具，感觉k6安装和使用都比较方便  step1：安装 # 在windows命令行工具输入winget install k6# 安装完成后，查看版本k6 --versionk6 v0.45.0 (2023-06-19T08:41:01&#43;0000/v0.45.0-0-gc3b4587b, go1.20.5, windows/amd64)Step2：使用 # 在项目根目录下创建文件夹pressureTest【自定义】# 在该文件夹下创建js文件，文件内容为压测的内容# k6 run xxx.jsStep3：示例  dataReceive.js
 import http from &#39;k6/http&#39;;import { check, sleep, group } from &#39;k6&#39;;export let options = {vus: 500, // 虚拟用户数 duration: &#39;30s&#39;, // 压测持续时间};export default function () {group(&#39;Example API&#39;, function () {// 定义请求的payload数据 let payload = JSON.">
<meta itemprop="datePublished" content="2024-08-06T17:40:12&#43;08:00" />
<meta itemprop="dateModified" content="2024-08-06T17:40:12&#43;08:00" />
<meta itemprop="wordCount" content="374">



<meta itemprop="keywords" content="" /><meta property="og:title" content="使用K6对接口进行压测" />
<meta property="og:description" content="使用K6对接口进行压测 背景  在实际开发中，常常需要对接口进行性能测试，因此，少不了压测工具
  在网上找了几款压测工具，感觉k6安装和使用都比较方便  step1：安装 # 在windows命令行工具输入winget install k6# 安装完成后，查看版本k6 --versionk6 v0.45.0 (2023-06-19T08:41:01&#43;0000/v0.45.0-0-gc3b4587b, go1.20.5, windows/amd64)Step2：使用 # 在项目根目录下创建文件夹pressureTest【自定义】# 在该文件夹下创建js文件，文件内容为压测的内容# k6 run xxx.jsStep3：示例  dataReceive.js
 import http from &#39;k6/http&#39;;import { check, sleep, group } from &#39;k6&#39;;export let options = {vus: 500, // 虚拟用户数 duration: &#39;30s&#39;, // 压测持续时间};export default function () {group(&#39;Example API&#39;, function () {// 定义请求的payload数据 let payload = JSON." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.lican.site/postsbak/%E4%BD%BF%E7%94%A8k6%E5%AF%B9%E6%8E%A5%E5%8F%A3%E5%8E%8B%E6%B5%8B/" />
<meta property="article:published_time" content="2024-08-06T17:40:12+08:00" />
<meta property="article:modified_time" content="2024-08-06T17:40:12+08:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="使用K6对接口进行压测"/>
<meta name="twitter:description" content="使用K6对接口进行压测 背景  在实际开发中，常常需要对接口进行性能测试，因此，少不了压测工具
  在网上找了几款压测工具，感觉k6安装和使用都比较方便  step1：安装 # 在windows命令行工具输入winget install k6# 安装完成后，查看版本k6 --versionk6 v0.45.0 (2023-06-19T08:41:01&#43;0000/v0.45.0-0-gc3b4587b, go1.20.5, windows/amd64)Step2：使用 # 在项目根目录下创建文件夹pressureTest【自定义】# 在该文件夹下创建js文件，文件内容为压测的内容# k6 run xxx.jsStep3：示例  dataReceive.js
 import http from &#39;k6/http&#39;;import { check, sleep, group } from &#39;k6&#39;;export let options = {vus: 500, // 虚拟用户数 duration: &#39;30s&#39;, // 压测持续时间};export default function () {group(&#39;Example API&#39;, function () {// 定义请求的payload数据 let payload = JSON."/>

	<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
	<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
	<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
	<link rel="manifest" href="/site.webmanifest">
	<link rel="mask-icon" href="/safari-pinned-tab.svg" color="">
	<link rel="shortcut icon" href="/favicon.ico">

	<title>使用K6对接口进行压测</title>
	<link rel="stylesheet" href="https://www.lican.site/css/style.min.d3141168199607bf3a517216ce3c263814eecdbc8fca72a9a88700799a838219.css">
	
</head>

<body id="page">
	
	<header id="site-header" class="animated slideInUp faster">
		<div class="hdr-wrapper section-inner">
			<div class="hdr-left">
				<div class="site-branding">
					<a href="https://www.lican.site">花落雨忧</a>
				</div>
				<nav class="site-nav hide-in-mobile">
					<a href="https://www.lican.site/posts">首页</a>
					<a href="https://www.lican.site/tech/">技术文档</a>
					<a href="https://www.lican.site/article/">文章</a>
					<a href="https://www.lican.site/project/">项目</a>
					<a href="https://www.lican.site/about/">关于</a>
				</nav>
			</div>
			<div class="hdr-right hdr-icons">
				<button id="menu-btn" class="hdr-btn" title="Menu"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg></button>
			</div>
		</div>
	</header>
	<div id="mobile-menu" class="animated fast">
		<ul>
			<li><a href="https://www.lican.site/posts">首页</a></li>
			<li><a href="https://www.lican.site/tech/">技术文档</a></li>
			<li><a href="https://www.lican.site/article/">文章</a></li>
			<li><a href="https://www.lican.site/project/">项目</a></li>
			<li><a href="https://www.lican.site/about/">关于</a></li>
		</ul>
	</div>


	<main class="site-main section-inner thin animated fadeIn faster">
		<h1>使用K6对接口进行压测</h1>
		<div class="content">
			<h3 id="使用k6对接口进行压测">使用K6对接口进行压测<a href="#使用k6对接口进行压测" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<h3 id="背景">背景<a href="#背景" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<blockquote>
<p>在实际开发中，常常需要对接口进行性能测试，因此，少不了压测工具</p>
</blockquote>
<ul>
<li>在网上找了几款压测工具，感觉k6安装和使用都比较方便</li>
</ul>
<h3 id="step1安装">step1：安装<a href="#step1安装" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">在windows命令行工具输入</span>
<span style="color:#a6e22e">winget</span> <span style="color:#a6e22e">install</span> <span style="color:#a6e22e">k6</span>
<span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">安装完成后</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">查看版本</span>
<span style="color:#a6e22e">k6</span> <span style="color:#f92672">--</span><span style="color:#a6e22e">version</span>
<span style="color:#a6e22e">k6</span> <span style="color:#a6e22e">v0</span><span style="color:#ae81ff">.45.0</span> (<span style="color:#ae81ff">2023</span><span style="color:#f92672">-</span><span style="color:#ae81ff">06</span><span style="color:#f92672">-</span><span style="color:#ae81ff">19</span><span style="color:#a6e22e">T08</span>:<span style="color:#ae81ff">41</span>:<span style="color:#ae81ff">01</span><span style="color:#f92672">+</span><span style="color:#ae81ff">0000</span><span style="color:#f92672">/</span><span style="color:#a6e22e">v0</span><span style="color:#ae81ff">.45.0</span><span style="color:#f92672">-</span><span style="color:#ae81ff">0</span><span style="color:#f92672">-</span><span style="color:#a6e22e">gc3b4587b</span>, <span style="color:#a6e22e">go1</span><span style="color:#ae81ff">.20.5</span>, <span style="color:#a6e22e">windows</span><span style="color:#f92672">/</span><span style="color:#a6e22e">amd64</span>)
</code></pre></div><h3 id="step2使用">Step2：使用<a href="#step2使用" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">在项目根目录下创建文件夹pressureTest</span><span style="color:#960050;background-color:#1e0010">【</span><span style="color:#a6e22e">自定义</span><span style="color:#960050;background-color:#1e0010">】</span>
<span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">在该文件夹下创建js文件</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">文件内容为压测的内容</span>
<span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">k6</span> <span style="color:#a6e22e">run</span> <span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">js</span>
</code></pre></div><h3 id="step3示例">Step3：示例<a href="#step3示例" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<blockquote>
<p>dataReceive.js</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">import</span> <span style="color:#a6e22e">http</span> <span style="color:#a6e22e">from</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">k6</span><span style="color:#f92672">/</span><span style="color:#a6e22e">http</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
<span style="color:#f92672">import</span> { <span style="color:#a6e22e">check</span>, <span style="color:#a6e22e">sleep</span>, <span style="color:#a6e22e">group</span> } <span style="color:#a6e22e">from</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">k6</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;

<span style="color:#a6e22e">export</span> <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">options</span> = {
  <span style="color:#a6e22e">vus</span>: <span style="color:#ae81ff">500</span>, <span style="color:#75715e">// 虚拟用户数
</span><span style="color:#75715e"></span>  <span style="color:#a6e22e">duration</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#ae81ff">30</span><span style="color:#a6e22e">s</span><span style="color:#960050;background-color:#1e0010">&#39;</span>, <span style="color:#75715e">// 压测持续时间
</span><span style="color:#75715e"></span>};

<span style="color:#a6e22e">export</span> <span style="color:#66d9ef">default</span> <span style="color:#a6e22e">function</span> () {
  <span style="color:#a6e22e">group</span>(<span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Example</span> <span style="color:#a6e22e">API</span><span style="color:#960050;background-color:#1e0010">&#39;</span>, <span style="color:#a6e22e">function</span> () {
    <span style="color:#75715e">// 定义请求的payload数据
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">payload</span> = <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>({
      <span style="color:#a6e22e">sn</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#ae81ff">3500000200000001</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
      <span style="color:#a6e22e">timestamp</span>: <span style="color:#ae81ff">1719904084</span>,
      <span style="color:#a6e22e">data</span>: [
        {
          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_p0</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">上层温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.1</span>,
          <span style="color:#a6e22e">status</span>: {
            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p0_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">上层温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          },
        },
        {
          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_p1</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">中层温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.2</span>,
          <span style="color:#a6e22e">status</span>: {
            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p1_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">中层温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          },
        },
        {
          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_p2</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">下层温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.3</span>,
          <span style="color:#a6e22e">status</span>: {
            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p2_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">下次温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          },
        },
        {
          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_in</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">空气温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.6</span>,
          <span style="color:#a6e22e">status</span>: {
            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p0_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">空气温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
          },
        },
      ],
    });

    <span style="color:#75715e">// 发送POST请求
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">res</span> = <span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">post</span>(<span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">https</span>:<span style="color:#75715e">//brewing-sass.nongbotech.com/api/device/data/receive&#39;, payload, {
</span><span style="color:#75715e"></span>      <span style="color:#a6e22e">headers</span>: {
        <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Content</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Type</span><span style="color:#960050;background-color:#1e0010">&#39;</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">json</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
      },
    });

    <span style="color:#75715e">// 检查响应状态码
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">check</span>(<span style="color:#a6e22e">res</span>, {
      <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">status</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200</span><span style="color:#960050;background-color:#1e0010">&#39;</span>: (<span style="color:#a6e22e">r</span>) =&gt; <span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">status</span> <span style="color:#f92672">==</span>= <span style="color:#ae81ff">200</span>,
    });

    <span style="color:#75715e">// 添加自定义指标
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">responseTime</span> = <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">timings</span>.<span style="color:#a6e22e">duration</span>;
    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">apiName</span> = <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Example</span> <span style="color:#a6e22e">API</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">tags</span> = { <span style="color:#a6e22e">endpoint</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#f92672">/</span><span style="color:#a6e22e">device</span><span style="color:#f92672">/</span><span style="color:#a6e22e">data</span><span style="color:#f92672">/</span><span style="color:#a6e22e">receive</span><span style="color:#960050;background-color:#1e0010">&#39;</span> };
    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">data</span> = {
      <span style="color:#a6e22e">responseTime</span>: <span style="color:#a6e22e">responseTime</span>,
    };
    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">sampleRate</span> = <span style="color:#ae81ff">0.5</span>; <span style="color:#75715e">// 采样率，用于控制报告中指标的采样数量
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">customMetrics</span> = {
      [<span style="color:#a6e22e">apiName</span>]: {
        <span style="color:#a6e22e">tags</span>,
        <span style="color:#a6e22e">data</span>,
        <span style="color:#a6e22e">sampleRate</span>,
      },
    };
    <span style="color:#75715e">// 将自定义指标发送到k6的输出
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// 根据需要可以将指标发送到InfluxDB等其他目标
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">customMetricPayload</span> = <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">customMetrics</span>);
    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">customMetricPayload</span>);

    <span style="color:#a6e22e">sleep</span>(<span style="color:#ae81ff">1</span>); <span style="color:#75715e">// 每个虚拟用户执行完后等待1秒钟
</span><span style="color:#75715e"></span>  });
}
</code></pre></div><h3 id="step4-查看报告">Step4: 查看报告<a href="#step4-查看报告" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<blockquote>
<p>进入dataReceive.js目录，执行 k6 run dataReceive.js</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"> <span style="color:#a6e22e">Example</span> <span style="color:#a6e22e">API</span>

       <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#a6e22e">status</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200</span>
        <span style="color:#960050;background-color:#1e0010">↳</span>  <span style="color:#ae81ff">0</span><span style="color:#f92672">%</span> <span style="color:#960050;background-color:#1e0010">—</span> <span style="color:#960050;background-color:#1e0010">✓</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">/</span> <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#ae81ff">8513</span>

     <span style="color:#a6e22e">checks</span><span style="color:#f92672">.....................</span>: <span style="color:#ae81ff">0.00</span><span style="color:#f92672">%</span>   <span style="color:#960050;background-color:#1e0010">✓</span> <span style="color:#ae81ff">0</span>          <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#ae81ff">8513</span>
     <span style="color:#a6e22e">data_received</span><span style="color:#f92672">............</span>..: <span style="color:#ae81ff">4.4</span> <span style="color:#a6e22e">MB</span>  <span style="color:#ae81ff">96</span> <span style="color:#a6e22e">kB</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">data_sent</span><span style="color:#f92672">..................</span>: <span style="color:#ae81ff">9.2</span> <span style="color:#a6e22e">MB</span>  <span style="color:#ae81ff">203</span> <span style="color:#a6e22e">kB</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">group_duration</span><span style="color:#f92672">............</span>.: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">1.96</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">1.01</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">1.03</span><span style="color:#a6e22e">s</span>   <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">24.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">4.56</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">6.01</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">http_req_blocked</span><span style="color:#f92672">.........</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">194.11</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">19.29</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">450.07</span><span style="color:#a6e22e">ms</span>
     <span style="color:#a6e22e">http_req_connecting</span><span style="color:#f92672">......</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">4.33</span><span style="color:#a6e22e">ms</span>   <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">5.46</span><span style="color:#a6e22e">s</span>  <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">47.26</span><span style="color:#a6e22e">ms</span>
     <span style="color:#a6e22e">http_req_duration</span><span style="color:#f92672">.........</span>.: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">676.84</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">16.98</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">23.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">2.39</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">4.33</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">http_req_failed</span><span style="color:#f92672">............</span>: <span style="color:#ae81ff">100.00</span><span style="color:#f92672">%</span> <span style="color:#960050;background-color:#1e0010">✓</span> <span style="color:#ae81ff">8513</span>       <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#ae81ff">0</span>
     <span style="color:#a6e22e">http_req_receiving</span><span style="color:#f92672">.........</span>: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">102.33</span><span style="color:#a6e22e">µs</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">4.53</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">504.29</span><span style="color:#a6e22e">µs</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">780.59</span><span style="color:#a6e22e">µs</span>
     <span style="color:#a6e22e">http_req_sending</span><span style="color:#f92672">.........</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">27.07</span><span style="color:#a6e22e">µs</span>  <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">2.52</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">55.85</span><span style="color:#a6e22e">µs</span>
     <span style="color:#a6e22e">http_req_tls_handshaking</span><span style="color:#f92672">...</span>: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">174.71</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">18.95</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">112.85</span><span style="color:#a6e22e">ms</span>
     <span style="color:#a6e22e">http_req_waiting</span><span style="color:#f92672">.........</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">676.71</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">16.82</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">23.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">2.39</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">4.33</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">http_reqs</span><span style="color:#f92672">..................</span>: <span style="color:#ae81ff">8513</span>    <span style="color:#ae81ff">187.091907</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">iteration_duration</span><span style="color:#f92672">.........</span>: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">1.96</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">1.01</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">1.03</span><span style="color:#a6e22e">s</span>   <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">24.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">4.56</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">6.01</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">iterations</span><span style="color:#f92672">...............</span>..: <span style="color:#ae81ff">8513</span>    <span style="color:#ae81ff">187.091907</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
     <span style="color:#a6e22e">vus</span><span style="color:#f92672">........................</span>: <span style="color:#ae81ff">1</span>       <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">1</span>        <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">500</span>
     <span style="color:#a6e22e">vus_max</span><span style="color:#f92672">..................</span>..: <span style="color:#ae81ff">500</span>     <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">500</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">500</span>
</code></pre></div><p><img src="https://www.lican.site/images/1722937145315.png" alt="1722937145315"></p>

		</div>
	</main>

	<footer id="site-footer" class="section-inner thin animated fadeIn faster">
		<p>&copy; 2024 <a href="https://www.lican.site"></a></p>
		<p>
			Made with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> &#183; Theme <a href="https://github.com/Track3/hermit" target="_blank" rel="noopener">Hermit</a> &#183; <a href="https://www.lican.site/post/index.xml" target="_blank" title="rss"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-rss"><path d="M4 11a9 9 0 0 1 9 9"></path><path d="M4 4a16 16 0 0 1 16 16"></path><circle cx="5" cy="19" r="1"></circle></svg></a>
		</p>
	</footer>


	<script src="https://www.lican.site/js/main.min.784417f5847151f848c339cf0acb13a06cbb648b1483435a28ed4556c4ead69b.js"></script>

</body>

</html>
